﻿Public Class MobileTypeManageForm

    Private Sub CompressorGroupManageForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'LoadDevTypeGroup()
    End Sub

    Private _pi As ProjectItem

    Private Sub LoadDevTypeGroup()

        _pi = MainForm.pi

        _isModified = False
        Dim bFirst As Boolean = True

        For Each dti As DevTypeItem In ProjectService.GetDevRefTypeList(_pi)
            AddGroupControl(dti)
        Next

        If Me.FlowLayoutPanel1.Controls.Count > 0 Then
            CType(Me.FlowLayoutPanel1.Controls(0), MobileTypeControl).groupSelected = True
            CType(Me.FlowLayoutPanel1.Controls(0), MobileTypeControl).Focus()
            Me.btnModifyGroup.Enabled = True
            Me.btnDeleteGroup.Enabled = True
        Else
            Me.btnModifyGroup.Enabled = False
            Me.btnDeleteGroup.Enabled = False
        End If

    End Sub

    Private Sub AddGroupControl(ByVal dti As DevTypeItem)
        Dim c As New MobileTypeControl()

        c.dtiItem = dti
        c.SetEnable(False)
        c.groupSelected = False

        AddHandler c.GroupSelectEvent, AddressOf CompressorGroupControl_GroupSelectEvent

        Me.FlowLayoutPanel1.Controls.Add(c)
    End Sub


    Private _isModified As Boolean
    Public Property IsModified() As Boolean
        Get
            Return _isModified
        End Get
        Set(ByVal value As Boolean)
            _isModified = value
        End Set
    End Property


    Private Sub btnAddGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGroup.Click
        Dim frm As New MobileTypeForm(String.Empty)
        Dim tip As String

        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then


            If True Then

                AddGroupControl(frm.dti)

                _isModified = True

                msg.Show("移动终端添加成功")
            Else
                tip = String.Format("增加移动终端【{0}】失败", frm.dti.NetId)
                msg.ShowError(tip)
            End If
        End If

    End Sub

    Private Sub CompressorGroupControl_GroupSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each c As MobileTypeControl In Me.FlowLayoutPanel1.Controls
            c.groupSelected = False
            c.SetEnable(False)
        Next

        CType(sender, MobileTypeControl).groupSelected = True
        'CType(sender, MobileTypeControl).SetEnable(True)
        Me.btnDeleteGroup.Enabled = True
        Me.btnModifyGroup.Enabled = True

    End Sub

    Private Function GetSelectedControl() As MobileTypeControl
        Dim item As MobileTypeControl = Nothing

        For Each c As MobileTypeControl In Me.FlowLayoutPanel1.Controls
            If c.groupSelected Then
                item = c
                Exit For
            End If
        Next

        Return item
    End Function

    Private Sub btnModifyGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifyGroup.Click
        'Dim ctl As MobileTypeControl
        'Dim tip As String

        'ctl = GetSelectedControl()

        'If ctl Is Nothing Then
        '    Exit Sub
        'End If

        'Dim frm As New MobileTypeForm(ctl.dtiItem, _pi.ProjectID)
        'If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
        '    ctl.dtiItem = frm.dti
        '    If ProjectService.UpdateDevRefTypeList(frm.dti) = True Then

        '        ''Log.log(tbccLogOperateType.ModifyProject, String.Format("修改设备{0}", frm.dti.Netid))

        '        _isModified = True

        '        '刷新工程配置
        '        MainForm.RefeshCurrentProjectInfo()
        '    Else
        '        tip = String.Format("修改设备【{0}】失败", frm.dti.NetId)
        '        msg.ShowError(tip)
        '    End If
        'End If
    End Sub

    Private Sub btnDeleteGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteGroup.Click
        Dim ctl As MobileTypeControl
        ctl = GetSelectedControl()

        If ctl Is Nothing Then
            Exit Sub
        End If

        Dim tip As String
        tip = String.Format("是否要删除移动终端【{0}】的所有配置信息？", ctl.dtiItem.NetId)
        If msg.ShowYesNo(tip) = Windows.Forms.DialogResult.Yes Then
            If True Then

                Me.FlowLayoutPanel1.Controls.Remove(ctl)

                ''Log.log(tbccLogOperateType.ModifyProject, String.Format("删除设备{0}", ctl.dtiItem.NetId))

                _isModified = True

                msg.Show("删除成功")
            Else
                tip = String.Format("删除移动终端【{0}】失败", ctl.dtiItem.NetId)
                msg.ShowError(tip)
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Public Shared NetID As Integer = 0

    Private Sub MobileTypeManageForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        MobileTypeManageForm.NetID = 0
    End Sub
End Class